<?php

namespace Admin\Controller;

/**
 * 题库控制器
 *
 * @author zhiyuan
 *        
 */
class ItemBankController extends AdminController {
	public function index() {
		$name = I ( 'name' );
		$map ['name'] = array (
				'like',
				'%' . $name . '%' 
		);
		if (! IS_ROOT) {
			$map ['users'] = array (
					'like',
					'%' . _UID_ . '%' 
			);
		}
		$list = $this->lists ( 'ItemBank', $map, 'id' );
		int_to_string ( $list );
		$this->assign ( 'list', $list );
		$this->meta_title = '题库管理';
		$this->display ();
	}
	public function add() {
		if (IS_POST) {
			$itembank = D ( 'ItemBank' );
			$data = $itembank->create ();
			if ($data) {
				if ($itembank->add ()) {
					$this->success ( '新增成功', U ( 'index' ) );
				} else {
					$this->error ( '新增失败' );
				}
			} else {
				$this->error ( $itembank->getError () );
			}
		} else {
			$this->meta_title = '新增题库';
			$this->display ();
		}
	}
	public function edit($id = 0) {
		if (IS_POST) {
			$itembank = D ( 'ItemBank' );
			$data = $itembank->create ();
			if ($data) {
				if ($itembank->save ()) {
					$this->success ( '更新成功', U ( 'index' ) );
				} else {
					$this->error ( '更新失败' );
				}
			} else {
				$this->error ( $itembank->getError () );
			}
		} else {
			$iteminfo = D ( 'ItemBank' )->field ( true )->find ( $id );
			if (false === $iteminfo) {
				$this->error ( '获取题库信息错误' );
			}
			$this->assign ( 'iteminfo', $iteminfo );
			$this->meta_title = '编辑题库';
			$this->display ();
		}
	}
	public function del() {
		$id = array_unique ( ( array ) I ( 'id', 0 ) );
		$id = is_array ( $id ) ? implode ( ',', $id ) : $id;
		if (empty ( $id )) {
			$this->error ( '请选择要操作的数据!' );
		}
		
		// 检索关联数据
		$Questionids = D ( 'Questions' )->where ( array (
				'itembank' => array (
						'in',
						$id 
				) 
		) )->field ( 'id' )->select ();
		if ($Questionids != null) {
			$this->error ( '有试题数据与题库关联，禁止删除题库!' );
		}
		
		$map = array (
				'id' => array (
						'in',
						$id 
				) 
		);
		if (D ( 'ItemBank' )->where ( $map )->delete ()) {
			$this->success ( '删除成功', U ( 'index' ) );
		} else {
			$this->error ( '删除失败！' );
		}
	}
	public function authorize() {
		$id = I ( 'id' );
		$iteminfo = D ( 'ItemBank' )->field ( 'name,users' )->find ( ( int ) $id );
		$department_list = C ( 'APP_DEPARTMENT_LIST' );
		$map ['uid'] = array (
				'neq',
				C ( 'USER_ADMINISTRATOR' ) 
		);
		$user_list = D ( 'Member' )->where ( $map )->field ( 'uid,nickname,realname,department' )->select ();
		int_to_string ( $user_list );
		$this->assign ( 'department_list', $department_list );
		$this->assign ( 'user_list', $user_list );
		$this->assign ( 'id', $id );
		$this->assign ( 'iteminfo', $iteminfo );
		$this->meta_title = '授权用户';
		$this->display ();
	}
	public function writeusers() {
		$id = ( int ) (I ( 'id' ));
		$users = ',' . (arr2str ( I ( 'users' ), ',' )) . ',';
		$itembank = D ( 'ItemBank' );
		$data = $itembank->create ( array (
				'users' => $users 
		) );
		if ($data) {
			$res = $itembank->where ( array (
					'id' => $id 
			) )->save ( $data );
			if ($res === false) {
				$this->error ( '操作失败' . $itembank->getError () );
			} else {
				$this->success ( '操作成功!', U ( 'index' ) );
			}
		} else {
			$this->error ( '操作失败' . $itembank->getError () );
		}
	}
	public function myitems() {
		$name = I ( 'name' );
		$map ['name'] = array (
				'like',
				'%' . $name . '%' 
		);
		if (! IS_ROOT) {
			$map ['users'] = array (
					'like',
					'%' . _UID_ . '%' 
			);
		}
		$list = $this->lists ( 'ItemBank', $map, 'id,name' );
		int_to_string ( $list );
		Cookie ( '__myitemsforward__', $_SERVER ['REQUEST_URI'] ); // 记录当前列表页的cookie
		$this->assign ( 'list', $list );
		$this->meta_title = '我的题库';
		$this->display ();
	}
}

?>